package com.peas.platform.module.icims.expenditure.initialize.domain.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.sys.enums.CommonYesNo;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import com.peas.platform.module.icims.expenditure.initialize.adapter.repository.ExpenditureInitReimbursementRepository;
import com.peas.platform.module.icims.expenditure.initialize.adapter.repository.po.ExpenditureInitReimbursementPo;
import com.peas.platform.module.icims.expenditure.initialize.domain.ExpenditureInitReimbursementDomain;
import com.peas.platform.module.icims.expenditure.initialize.dto.ExpenditureInitReimbursementDto;
import com.peas.platform.module.icims.expenditure.kpis.dto.ExpenditureKpiDto;
import com.peas.platform.module.icims.expenditure.kpis.enums.ExpenditureKpiState;
import com.peas.platform.module.icims.expenditure.kpis.service.ExpenditureKpiService;
import com.peas.platform.module.icims.expenditure.setting.dto.ExpenditureSettingsDto;
import com.peas.platform.module.icims.expenditure.setting.enums.ExpenditureSettingsType;
import com.peas.platform.module.icims.expenditure.setting.service.ExpenditureSettingsService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Component
public class ExpenditureInitReimbursementDomainImpl extends BaseDomainImpl<ExpenditureInitReimbursementDto, ExpenditureInitReimbursementPo> implements ExpenditureInitReimbursementDomain {

    @Resource
    private ExpenditureInitReimbursementRepository expenditureInitReimbursementRepository;
    @Resource
    private SequenceService sequenceService;
    @Resource
    private ExpenditureKpiService expenditureKpiService;
    @Resource
    private ExpenditureSettingsService expenditureSettingsService;

    @Override
    public BaseRepository<ExpenditureInitReimbursementPo> getMapper() {
        return expenditureInitReimbursementRepository;
    }

    @Override
    public Class<? extends BaseRepository<ExpenditureInitReimbursementPo>> getRepositoryClass() {
        return ExpenditureInitReimbursementRepository.class;
    }

    private void checkKpi(Long expenditureKpiId) {
        // 检查指标单
        List<ExpenditureSettingsDto> esds = expenditureSettingsService.listByField("settingsType", ExpenditureSettingsType.kpiControl.getKey());
        if (CollUtil.isNotEmpty(esds) && StrUtil.equals(CommonYesNo.yes.getKey(), esds.get(0).getSettingsValue())) {
            ExpenditureKpiDto kpi = expenditureKpiService.getById(expenditureKpiId);
            Assert.equals(ExpenditureKpiState.pass.getKey(), kpi.getState(), "所选指标单已经开始执行业务了，不能再初始化了");
        }
    }

    @Transactional
    @Override
    public int save(ExpenditureInitReimbursementDto dto) {
        Assert.notNull(dto.getExpenditureKpiId(), "请选择指标单");
        Assert.isTrue(dto.getAmount().compareTo(BigDecimal.ZERO) > 0, "初始报账金额必须大于0");
        checkKpi(dto.getExpenditureKpiId());// 检查指标单
        int result = super.save(dto);
        this.checkRepetition("reimbursementCode", dto.getReimbursementCode());
        Long expenditureKpiId = dto.getExpenditureKpiId();
        Assert.isTrue(expenditureKpiService.queryAvailableAmount(expenditureKpiId).compareTo(BigDecimal.ZERO) >= 0, "初始报账金额不能超过指标单可用金额");
        return result;
    }

    @Transactional
    @Override
    public int updateById(ExpenditureInitReimbursementDto dto) {
        Assert.notNull(dto.getExpenditureKpiId(), "请选择指标单");
        Assert.isTrue(dto.getAmount().compareTo(BigDecimal.ZERO) > 0, "初始报账金额必须大于0");
        checkKpi(dto.getExpenditureKpiId());// 检查指标单
        int result = super.updateById(dto);
        this.checkRepetition("reimbursementCode", dto.getReimbursementCode());
        return result;
    }

}
